/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Bussiness;
import Entity.E_Staff;
import Entity.E_Admin;
import Entity.E_Customer;
import DatabaseAccess.D_DBHelper;
import javax.swing.JOptionPane;
import java.sql.ResultSet;
import java.util.Vector;
import GUI.CustomerForm;
import GUI.DepartmentForm;
import GUI.Login;
import GUI.ManagerForm;
import GUI.OrderManagerForm;
/**
 *
 * @author thao
 */
public class B_CheckLogin {
    public D_DBHelper dbHelper=null;
    public ResultSet rsAdmin=null;
    public ResultSet rsStaff=null;
    public ResultSet rsCus=null;
    public Vector userPass=null;
    public CustomerForm cus=null;
    public DepartmentForm dep=null;
    public E_Customer enCustomer=null;
    public E_Admin enAdmin=null;
    public E_Staff enStaff;
    public boolean found=false;
    public OrderManagerForm ordermanager = null;
    public B_CheckLogin(){
        dbHelper=new D_DBHelper();
    }
    public void checkLoginClient(String user,String pass){
        try{
         dbHelper.setConnectionUrl("connection.cfg");
        }catch(Exception e){
            e.printStackTrace();
        }
        if(!dbHelper.setupConnection()){
              JOptionPane.showMessageDialog(null,"Can not open the connection","Connection Dialog",JOptionPane.ERROR_MESSAGE);
         }else{
            userPass=new Vector();
            userPass.add(user);
            userPass.add(pass);
            userPass.add(1);
           try{
            rsAdmin=dbHelper.executeProcedure("CheckLogin",userPass);
            userPass.removeElementAt(2);
            userPass.add(2,2);
            rsStaff=dbHelper.executeProcedure("CheckLogin",userPass);
            userPass.removeElementAt(2);
            userPass.add(2,3);
            rsCus=dbHelper.executeProcedure("CheckLogin",userPass);
            if(rsAdmin.next()){
                System.out.println("Admin");
                ManagerForm manager = new ManagerForm(rsAdmin.getInt(1), rsAdmin.getString(4));
                manager.setVisible(true);
                found=true;
            }
            if(rsStaff.next()){
                found=true;

                if(rsStaff.getInt(3)==3){
                    enStaff=new E_Staff(rsStaff.getInt(1),rsStaff.getString(2),
                           rsStaff.getInt(3),rsStaff.getString(4),rsStaff.getString(5));
                    ordermanager = new OrderManagerForm();
                    ordermanager.setVisible(true);
                    System.out.println("Staff");
                }else{
                    enStaff=new E_Staff(rsStaff.getInt(1),rsStaff.getString(2),
                           rsStaff.getInt(3),rsStaff.getString(4),rsStaff.getString(5));
                    dep=new DepartmentForm();
                    dep.setVisible(true);
                    System.out.println("Staff");
                    //dep.setStaff(enStaff.getStaffID(),enStaff.getStaffUsername());
                }

            }
            if(rsCus.next()){
                System.out.println("Customer");
                found=true;
                cus=new CustomerForm(rsCus.getInt(1), rsCus.getString(2));
                cus.setVisible(true);
            }

            if(!found){
              JOptionPane.showMessageDialog(null,"Account invaild ! ","Check Login Dialog",JOptionPane.ERROR_MESSAGE);
              new Login(null, true).setVisible(true);
            }
            }catch(Exception e){
                e.printStackTrace();
            }
          }
    }

}
